var Context = {
		'controls':sys_element_create("div",{
			'id':'border',
			'dimension':'h:pageHeight;w:pageWidth;',
			'overall':sys_element_create("div",{
				'id':'overall',
				'addContent':function(content,attribute){
					var browsersize=sys_browser_getSize();
					Context.controls.overall.style.height = (browsersize.height)+'px';
					Context.controls.overall.style.width = (browsersize.width) + 'px';
					Context.controls.overall.style.zIndex = 500;
					var w = Context.controls.overall.offsetWidth;
					var h = Context.controls.overall.offsetHeight;
					var e = sys_element_create('div',{
						'className':'overall_content_border',
						'style':{
							'width':w+'px',
							'height':h+'px',
							'position':'fixed',
							'top':'0px',
							'left':'0px',
							'z-index':501
						}
					},true);
					var contentborder = sys_element_create('div',{
						'className':'overall_content',
						'style':{'position':'absolute'}
					},true);
					if(content.innerHTML){
						sys_element_dimension(content);
						contentborder.appendChild(content);
					}else{
						contentborder.innerHTML = content;
					}
					if(attribute){
						if(attribute.type && attribute.type == 'cancel/ok'){
							var closebtn = sys_element_create('div',{
								'className':'overall_content_cancelbtn',
								'span':sys_element_create("div",{
									'innerHTML':'Close'
								}),
								'action':attribute.onClose
							},true);
							var submitbtn = sys_element_create('div',{
								'className':'overall_content_submitbtn',
								'span':sys_element_create('div',{
									'innerHTML':'Ok'
								}),
								'action':attribute.onOk
							},true);
							sys_event_attach('click',closebtn,function(evt){
								Context.controls.overall.innerHTML = '';
								Context.controls.overall.style.height = (0)+'px';
								Context.controls.overall.style.width = (0) + 'px';
								var actor = sys_event_getActor(evt);
								if(actor.action){
									action();
								}
							});
							sys_event_attach('click',submitbtn,function(evt){
								Context.controls.overall.close();
								var actor = sys_event_getActor(evt);
								if(actor.action){
									action();
								}
							});
							contentborder.appendChild(closebtn);
							contentborder.appendChild(submitbtn);
						}else if(attribute.type && attribute.type == 'close'){
							var closebtn = sys_element_create('div',{
								'className':'overall_content_closebtn',
								'span':sys_element_create("div",{
									'innerHTML':'Close'
								}),
								'action':attribute.onClose
							},true);
							sys_event_attach('click',closebtn,function(evt){
								Context.controls.overall.innerHTML = '';
								Context.controls.overall.style.height = (0)+'px';
								Context.controls.overall.style.width = (0) + 'px';
							});
							contentborder.appendChild(closebtn);
						}
					}
					e.appendChild(contentborder);
					Context.controls.overall.appendChild(e);
					contentborder.style.top = ((h - contentborder.offsetHeight)/3) + 'px';
					contentborder.style.left = ((w - contentborder.offsetWidth)/2) + 'px';
				},
				'close':function(){
					Context.controls.overall.innerHTML = '';
					Context.controls.overall.style.height = (0)+'px';
					Context.controls.overall.style.width = (0) + 'px';
				}
			},true),
			'content':sys_element_create("div",{
				'id':'content',
				'style':{'position':'relative'},
				'dimension':'w:1024;h:parentWidth;',
				'result':sys_element_create("div",{
					'id':'result',
					'style':{'background':'#5cb0e5'},
					'dimension':'h:parentHeight;w:parentWidth/100*25-1',
					'result_bar':sys_element_create("div",{
						'id':'result_bar',
						'dimension':'h:90;w:parentWidth'
					},true),
					'result_log':sys_element_create("div",{
						'id':'result_log',
						'dimension':'h:parentHeight - 90;w:parentWidth',
						'innerHTML':'bar',
						'style':{
							'overflow-y':'hidden'
						}
					})
				},true),
				'main':sys_element_create("div",{
					'id':'main',
					'dimension':'h:parentHeight;w:parentWidth/100*75;',
					'tool_bar':sys_element_create('div',{
						'id':'tool_bar',
						'dimension':'w:parentWidth;h:35;',
						'tabs':sys_element_create('div',{
							'id':'tabs_container',
							'dimension':'h:parentHeight'
						}),
						'cmds':sys_element_create('div',{
							'id':'cmds_container'
						}),
						'br':sys_element_create('br',{'className':'clear'},true)
					},true),
					'working_area':sys_element_create('div',{
						'id':'working_area',
						'dimension':'h:parentHeight - 30;w:parentWidth;'
					}, true),
					'br':sys_element_create("br",{'className':'clear'})
				},true),
				'br':sys_element_create("br",{'className':'clear'}),
			},true),
			'user_bar':sys_element_create('div',{
				'id':'user_bar',
				'dimension':'w:(pageWidth-1024)/2+(1024/100*25);h:95;x:0;y:0;',
				'style':{
					'position':'absolute',
					'left':'0px',
					'top':'0px',
					'margin':'0px',
					'background':"url('images/user_bar_bg.png') repeat-x"
				},
				'user_info':sys_element_create('div',{
					'dimension':'w:parentWidth;h:parentHeight;',
					'style':{
						'background':"url('images/user_bar_glys.png') no-repeat right top",
					}
				},true)
			},true)
		},true),
		'pages':{},
		'tabs':{},
		'loadTabs':function(){
			var rs = API.request("service","xml","appid=0&request=getTabs");
			var tabs = API.getResultMessages(rs);
			this.controls.content.main.tool_bar.tabs.innerHTML = '';
			for(var i = 0; i< tabs.length; i++){
				if(parseInt(tabs[i].name) >= 0){
					var tabinfo = tabs[i];
					var tab = sys_element_create("div",{
						'caption':sys_element_create("div",{
							'innerHTML':tabinfo.message
						}),					
						'className':'tab_inactive',
						'appid': parseInt(tabinfo.name)
					},true);
					tab.caption.parent = tab;
					this.tabs[tabinfo.name] = tab;
					var action = function(evt){
						var actor = sys_event_getActor(evt);
						if(actor.parent)actor = actor.parent;
						Context.activePage(actor.appid);
					};
					sys_event_attach('click',tab,action);
					sys_event_attach('click',tab.caption,action);
					this.controls.content.main.tool_bar.tabs.appendChild(tab);
				}
			}
			this.controls.content.main.tool_bar.tabs.appendChild(sys_element_create("br",{"className":"clear"}));
		},
		'loadPages':function(appid){
			if(this.pages[appid]){
			}else{
				var rs = API.request("getPage","xml","appid="+appid);
				this.controls.content.main.working_area.innerHTML = '';
				this.pages[appid] = sys_element_create("div",{
					'className':'page_inactive',
					'dimension':'h:parentHeight;w:parentWidth;',
					'innerHTML':API.getResultMessage(rs,"content"),
					'resize':function(page_obj){
						sys_element_dimension(page_obj);
					}
				},true);
				this.controls.content.main.working_area.appendChild(this.pages[appid]);
				sys_element_dimension(this.pages[appid]);
			}
		},
		'activePage':function(appid){
			if(this.pages[appid] && this.pages[appid].className == 'page_active')return;
			this.loadPages(appid);
			for(var tab in this.tabs){
				this.tabs[tab].className = "tab_inactive";
			}
			this.tabs[appid].className = "tab_active";
			for(var page in this.pages){
				this.pages[page].className = "page_inactive";
				this.pages[page].style.width = 0 + 'px';
				this.pages[page].style.height = 0 + 'px';
			}
			this.controls.content.main.working_area.appendChild(this.pages[appid]);
			this.pages[appid].className = "page_active";
			sys_element_dimensionWithCommand(this.pages[appid],"h:parentHeight;w:parentWidth");
			this.pages[appid].resize(this.pages[appid]);
		},
		'removeAllPages':function(){
			this.pages = {};
			this.controls.content.main.working_area.innerHTML = '';
		},
		'publishContent':function(content,receiver){
			receiver.innerHTML = '';
			if(content.innerHTML){
				receiver.appendChild(content);
			}else{
				receiver.innerHTML = content;
			}
			sys_element_dimension(receiver);
		},
		'dialog':{
			'request':function(appid,formid,receiver){
				var rs = API.request("getForm","xml","appid="+appid+"&form_id="+formid);
				rs = API.getResultMessage(rs,"content");
				if(receiver == null){
					Context.showDialog(rs,{'type':'close'});
				}else{
					Context.publishContent(rs,receiver);
				}
			},
			'requestWithParam':function(appid,formid,param,receiver){
				var rs = API.request("getForm","xml","appid="+appid+"&form_id=" + formid + "&"+param);
				rs = API.getResultMessage(rs,"content");
				if(receiver == null){
					Context.showDialog(rs,{'type':'close'});
				}else{
					Context.publishContent(rs,receiver);
				}
			},
			'createConfirmForm':function(){
				var frm = sys_element_create("div",{
					'email_address':sys_element_create("div",{
						'title':sys_element_create("div",{'innerHTML':'Email address'}),
						'input':sys_element_create("input",{'name':'email_address','type':'text'})
					},true),
					'confirm_code':sys_element_create("div",{
						'title':sys_element_create("div",{'innerHTML':'Confirm code'}),
						'input':sys_element_create("input",{'name':'confirm_code','type':'text'})
					},true),
					'submit':sys_element_create("div",{
						'input':sys_element_create("button",{
							'innerHTML':'Confirm'
						})
					},true)
				},true);
				frm.submit.input.parent = frm;
				sys_event_attach('click',frm.submit.input,function(evt){var parent = sys_event_getActor(evt).parent; Context.confirm(parent.email_address.input.value,parent.confirm_code.input.value)});
				return frm;
			},
			'createWelcomeForm':function(){
				var frm = sys_element_create("div",{
					'dimension':'w:pageWidth/2;',
					'signup_section':sys_element_create("div",{
						'dimension':'w:parentWidth/100*50-1;',
						'style':{'float':'left','position':'relative'},
						'title':sys_element_create("div",{'innerHTML':'Signup','className':'title'}),
						'email_title':sys_element_create("div",{'innerHTML':'Email address'}),
						'email_input':sys_element_create("input",{'type':'text'}),
						'pasword_title':sys_element_create("div",{'innerHTML':'Password'}),
						'password_input':sys_element_create("input",{'type':'password'}),
						'submit':sys_element_create("div",{
							'input':sys_element_create("button",{'innerHTML':'Signup'})
						},true)
					},true),
					'login_section':sys_element_create("div",{
						'dimension':'w:parentWidth/100*50-1;',
						'style':{'float':'left','position':'relative'},
						'title':sys_element_create("div",{'innerHTML':'Login','className':'title'}),
						'email_title':sys_element_create("div",{'innerHTML':'Email address'}),
						'email_input':sys_element_create("input",{'type':'text'}),
						'pasword_title':sys_element_create("div",{'innerHTML':'Password'}),
						'password_input':sys_element_create("input",{'type':'password'}),
						'submit':sys_element_create("div",{
							'input':sys_element_create("button",{'innerHTML':'Login'})
						},true)
					},true),
					'br':sys_element_create('br',{'className':'clear'},true),
					'tool_bar':sys_element_create("div",{
						'dimension':'h:30',
						'confirm':sys_element_create("a",{"href":"#","innerHTML":"Confirm Email"})
					},true)
				},true);
				frm.signup_section.submit.input.parent = frm;
				frm.login_section.submit.input.parent = frm;
				sys_event_attach('click',frm.signup_section.submit.input,
					function(evt){
						parent=sys_event_getActor(evt).parent;
						Context.signup(parent.signup_section.email_input.value,parent.signup_section.password_input.value);
						Context.controls.overall.close();
					}
				);
				sys_event_attach('click',frm.login_section.submit.input,
					function(evt){
						parent = sys_event_getActor(evt).parent;
						if(Context.login(parent.login_section.email_input.value,parent.login_section.password_input.value))
						{
							Context.controls.overall.close();
						}
					}
				);
				sys_event_attach('click',frm.tool_bar.confirm,
					function(evt){Context.showDialog(Context.dialog.createConfirmForm());}
				);
				return frm;
			}
		},
		'showDialog':function(frm,attribute){
			this.controls.overall.addContent(frm,attribute);
		},
		'resize':function(){
			sys_element_dimension(this.controls);
		},
		'getStatistic':function(){
			var rs = API.request("statistic","xml","");
			this.controls.content.main.statistic.innerHTML = API.getResultMessage(rs,"content");
		},
		'getMainPage':function(){
			var rs = API.request("service","xml","id=mainpage");
			this.controls.content.main.working_area.innerHTML = API.getResultMessage(rs,"content");
		},
		'logout':function(){
			if(API.logout){
				this.removeAllPages();
				this.showDialog(this.dialog.createWelcomeForm());
			}
		},
		'login':function(email_address,password){
			if(API.login(email_address,password)){
				this.loadTabs();
				this.activePage(0);
				return true;
			}
			return false;
		},
		'signup':function(email_address,password){
			var rs = API.request("signup",'xml',"email_address="+email_address+"&password="+password);
			if(API.getResultStatus(rs) == 'true'){
			}else{
			}
		},
		'confirm':function(email_address,confirm_code){
			var rs = API.request("confirm",'xml',"email_address="+email_address+"&confirm_code="+confirm_code);
			if(API.getResultStatus(rs) == 'true'){
				this.showDialog(this.dialog.createWelcomeForm());
			}else{
			}
		},
		'contentRequest':function(method,obj){
			var param = sys_element_toParam(obj);
			var rs = API.request(method,'xml',param);
			this.controls.content.main.working_area.innerHTML = API.getResultMessage(rs,'content');
		},
		'freeRequest':function(method,param,receiver){
			var rs = API.request(method,'xml',param);
			if(receiver != null && receiver.innerHTML != null){
				receiver.innerHTML = API.getResultMessage(rs,'content');
				sys_element_dimension(receiver);
			}
		},
		'log':function(rs){
			if(rs && rs.status != null && rs.status != ""){
				var display = sys_element_create("div",{"style":{
					'border-top':'1px dotted',
					'background':(rs.status=='true'?'transparent':'#87c5ee'),
					'padding':'1px',
					'z-index':'0'
				}},true);
				if(rs.errors != null){
					display.error = sys_element_create("div",{"style":{"position":"relative"}},true);
					for(var i = 0; i < rs.errors.length; i++){
						display.error.appendChild(sys_element_create("div",{
							'code':sys_element_create("div",{'innerHTML':'error:'+rs.errors[i].code,'className':'error_code'}),
							'message':sys_element_create("div",{'innerHTML':rs.errors[i].message,'className':'error_message'})
						},true));
					}
					display.appendChild(display.error);
				}
				if(rs.messages!=null){
					display.message = sys_element_create("div",{"style":{"position":"relative"}},true);
					for(var i = 0; i< rs.messages.length; i++){
						if(rs.messages[i].name == 'content')continue;
						display.message.appendChild(sys_element_create("div",{
							'code':sys_element_create("div",{
								'innerHTML':rs.messages[i].name,
								'className':'message_title'},true),
							'message':sys_element_create("div",{
								'innerHTML':rs.messages[i].message,
								'className':'message_content'})
						},true));
					}
					display.appendChild(display.message);
				}
				display.appendChild(sys_element_create("br",{"className":"clear"}));
				Context.controls.content.result.result_log.appendChild(display);
				Context.controls.content.result.result_log.scrollTop = Context.controls.content.result.result_log.scrollHeight;
			}
		},
		'init':function(){
			sys_element_appendRoot(this.controls);
			API.resultListener = this.log;
			if(API.checkLogin()){
				this.loadTabs();
				this.activePage(0);
			}else{
				this.showDialog(this.dialog.createWelcomeForm());
			}
		},
		'variable':{
			'gid':-1
		}
	};